package com.pig4cloud.pig.generator.config;

import cn.hutool.core.date.DateUtil;
import com.pig4cloud.pig.generator.entity.SmsFlashPromotion;
import com.pig4cloud.pig.generator.service.SmsFlashPromotionService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.sql.Time;
import java.util.HashMap;
import java.util.List;


@Slf4j
@Component
public class ShoppingJobHandler {
	@Autowired
	SmsFlashPromotionService smsFlashPromotionService;

	@Autowired
	RedisTemplate redisTemplate;

	@XxlJob("shoppingJobHandler")
	public ReturnT<String> shoppingJobHandler(String s){
		redisTemplate.delete("miaosha");
		int i = DateUtil.thisHour(true);
		System.out.println(i+1);
	/*	if(i%2==0){
			i=i+2;
		}else{
			i=i+1;
		}*/
		if(i%2==0){

		}else{
			i=i-1;
		}
		String s1 = String.valueOf(i);
		int length = s1.length();
		s1=s1+":00:00";
		System.out.println(length);
		SmsFlashPromotion smsFlashPromotion=new SmsFlashPromotion();
		Time time = Time.valueOf(s1);
		smsFlashPromotion.setStartDate(time);
		System.out.println("开始时间:"+time);
		List<SmsFlashPromotion> list= smsFlashPromotionService.pmslist(smsFlashPromotion);
		System.out.println(list);
		HashMap<String,Integer> map=new HashMap<>();
		HashOperations hashOperations = redisTemplate.opsForHash();
		for (SmsFlashPromotion sms:list){
			map.put(String.valueOf(sms.getId()),sms.getNum());
		}
		hashOperations.putAll("miaosha",map);
		return ReturnT.SUCCESS;

	}
}
